<?php

namespace app\index\controller;

use think\Request;

use app\index\model\Topic as TopicModel;
use app\index\model\Reply as ReplyModel;
use app\index\model\Category as CategoryModel;

class Topic extends Core
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $param = $request->only(['order'], 'get');
        $paginate = TopicModel::minePaginate($param);
        $this->assign('paginate', $paginate);
        return $this->fetch('index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $categories = CategoryModel::all();
        $this->assign('categories', $categories);
        $this->assign('topic', ['category_id' => 0]);
        return $this->fetch('form');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if(!$request->isAjax()){
            $this->redirect('user/create');
        }

        $data = $request->post();
        $result = TopicModel::createItem($data);
        if($result->status){
            $data = $result->data;
            $this->success($result->message, url('topic/read', ['id' => $data['id']]));
        }else{
            $this->error($result->message);
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $topic = TopicModel::with(['user' => function($query){
            $query->field('id, name, avatar');
        }])->find($id);

        if(empty($topic)){
            $this->redirect('topic/index');
        }

        $reply_paginate = ReplyModel::minePaginate(['topic_id' => $topic->id]);

        $this->assign('topic', $topic);
        $this->assign('reply_paginate', $reply_paginate);
        return $this->fetch('read');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $topic = TopicModel::find($id);
        if(empty($topic)){
            $this->redirect('topic/index');
        }else if($topic->user_id != $this->current_user->id){
            $this->redirect('topic/index');
        }

        $this->assign('topic', $topic);

        $categories = CategoryModel::all();
        $this->assign('categories', $categories);
        return $this->fetch('form');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if(!$request->isAjax()){
            $this->redirect('user/read', ['id' => $id]);
        }

        $data = $request->post();
        $result = TopicModel::updateItem($id, $data);
        if($result->status){
            $data = $result->data;
            $this->success($result->message, url('topic/read', ['id' => $data['id']]));
        }else{
            $this->error($result->message);
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request, $id)
    {
        $param = $request->post();
        $result = TopicModel::deleteItem($id, $param);
        if($result->status){
            $this->success($result->message, url('topic/index'));
        }else{
            $this->error($result->message);
        }
    }
}
